package peng.yang.express.Adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.File;
import java.util.List;

import peng.yang.express.Bean.ExpressMember;
import peng.yang.express.Bean.ListRes;
import peng.yang.express.Bean.PhoneMembers;
import peng.yang.express.R;
import peng.yang.express.Util.HistoryMembersDao;
import peng.yang.express.Util.PhoneMembersDao;
import peng.yang.express.Util.RecycleImageView;

public class SwipeListViewBaseAdapter extends BaseAdapter {

    final String TAG = " SwipeAdapter";
    Context context;
    private List<ExpressMember> members;
    private LayoutInflater inflater;
    private LinearLayout noHistory;

    /**
     * 构造方法
     * @param context
     */
    public SwipeListViewBaseAdapter(Context context,LinearLayout noHistory) {
        inflater = LayoutInflater.from(context);
        members = ListRes.getMemeber(context);
        Log.e(TAG, members.toString());
        this.context = context;
        this.noHistory = noHistory;

    }

    public List<ExpressMember> getMembers(){
        return members;
    }
    /**
     * 得到简称名字
     * @param position
     * @return
     */

    public String getSigleName (int position){
        PhoneMembersDao dao = new PhoneMembersDao(context);
        PhoneMembers bean= dao.queryExpressexpName(members.get(position).getExpName());
        String simpleName = bean.getSimpleName().toString();
        return simpleName;
    }

    /**
     * 取得电话
     * @param position
     * @return
     */
    public String getNu(int position){
        return members.get(position).getNum();
    }

    /**
     * 取得商品名字
     * @param position
     * @return
     */
    public String getGoodName(int position){
        return members.get(position).getGoodname();
    }


    @Override
    public int getCount() {

        return members.size();
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Object getItem(int position) {
        return members.get(position);
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHandler handler = new ViewHandler();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.swipelist_item, null);

            handler.imageId = (RecycleImageView) convertView.findViewById(R.id.iv_check_history_image);
            handler.expressname = (TextView) convertView.findViewById(R.id.tv_history_title);
            handler.num = (TextView) convertView.findViewById(R.id.tv_history_num);
            handler.state = (TextView) convertView.findViewById(R.id.tv_history_state);
            handler.goodname = (TextView) convertView.findViewById(R.id.tv_history_name);

            Button remove = (Button) convertView.findViewById(R.id.remove);
            remove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    HistoryMembersDao dao = new HistoryMembersDao(context);
                    dao.delete(members.get(position).getNum());
                    members = dao.getAllMembers();
                    if(members.size()==0){
                        noHistory.setVisibility(View.VISIBLE);
                    }
                    notifyDataSetChanged();
                }
            });
            convertView.setTag(handler);
        } else {
            handler = (ViewHandler) convertView.getTag();
        }

        if (members.get(position).getExpName() != null) {
            handler.expressname.setText(members.get(position).getExpName());
        } else {
            handler.expressname.setText("");
        }
        if (members.get(position).getNum() != null) {
            handler.num.setText(members.get(position).getNum());
        } else {
            handler.num.setText("");
        }
        if (members.get(position).getState() != null) {
            handler.state.setText(members.get(position).getState());
        } else {
            handler.state.setText("");
        }
        if (members.get(position).getGoodname() != null) {
            handler.goodname.setText(members.get(position).getGoodname());
            handler.goodname.setVisibility(View.INVISIBLE);
        } else {
            handler.goodname.setText("");
        }

        if (members.get(position).getFileImageUrl() != null) {
            String path = members.get(position).getFileImageUrl();
            Log.e(TAG,path);
            File file = new File(context.getFilesDir(), path);
            Bitmap bitmap1 = BitmapFactory.decodeFile(file.getAbsolutePath());
            handler.imageId.setImageBitmap(bitmap1);
        } else {
            handler.goodname.setText("");
        }
        return convertView;
    }

    static class ViewHandler {
        private RecycleImageView imageId;
        private TextView expressname;
        private TextView num;
        private TextView state;
        private TextView goodname;

    }

}




